/**
 * 
 */
package com.kelaite.procurement.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kelaite.procurement.common.RetCodeEnum;
import com.kelaite.procurement.constants.Constants;
import com.kelaite.procurement.dto.CompanyDTO;
import com.kelaite.procurement.dto.UserDTO;
import com.kelaite.procurement.exception.BusinessException;
import com.kelaite.procurement.restful.BaseController;
import com.kelaite.procurement.service.UserService;
import com.kelaite.procurement.utils.CurrentUserUtils;

/**
 * @author yuanchangwei
 *
 */
@Controller
public class IndexController extends BaseController {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String homepage() {
		return "index";
	}

	@RequestMapping(value = "/preLogin", method = RequestMethod.GET)
	public String login(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		model.addAttribute("user", new UserDTO());
		model.addAttribute("source",
				StringUtils.trimToEmpty(request.getParameter("source")));
		return "login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(UserDTO user, RedirectAttributes redirect,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			user = userService.login(user);
			CurrentUserUtils.getInstance().setUser(user);
		} catch (Exception e) {
			logger.error(e);
			if (e instanceof BusinessException) {
				redirect.addFlashAttribute(Constants.ERROR_MSG, e.getMessage());
			} else {
				redirect.addFlashAttribute(Constants.ERROR_MSG,
						RetCodeEnum.SYSTEM_EXCEPTION.getMsg());
			}
			return "redirect:/preLogin.htm";
		}
		String source = StringUtils.trimToEmpty(request.getParameter("source"));
		if (StringUtils.isNotBlank(source)) {
			return "redirect:"
					+ new String(Base64Utils.decodeFromString(source));
		} else {
			CompanyDTO company = user.gettCompany();
			if (null != company) {
				if (StringUtils
						.equals(String.valueOf(company.getBusinessType()),
								String.valueOf(Constants.COMPANY_BUSINESS_TYPE_PARTY_SECOND))&&user.getType()!=4) {
					return "redirect:supplier/index.htm";
				} else if(user.getType()==Constants.COMPANY_BUSINESS_TYPE_PARTY_ADMIN){
					return "redirect:admin/index.htm";
				}else{
					return "redirect:client/index.htm";
				}
			} else {
				logger.error("user have no related compay info."
						+ user.toString());
				return "redirect:/preLogin.htm";
			}
		}
	}

	@RequestMapping(value = "/loginOut", method = RequestMethod.GET)
	public String loginOut(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			HttpSession session = request.getSession(false);// 防止创建Session
			if (null != session) {
				session.removeAttribute(Constants.CURRENT_USER);
			}
		} catch (Exception e) {
			logger.error(e);
		}
		return "redirect:/preLogin.htm";
	}

	@Override
	protected Class<?> getCurrentClass() {
		return IndexController.class;
	}
}
